﻿CREATE PROCEDURE [acms].[PostTopic_Put]
	@PostVersionId int, 
	@TopicId int,
	@ConcurencyId rowversion,
	@PostNameOut varchar(100) output,
	@PostDisplayNameOut varchar(100) output,
	@TopicNameOut varchar(100) output,
	@ConcurencyIdOut rowversion output
AS
	if not Exists(select null from PostTopics where PostVersionId = @PostVersionId and TopicId = @TopicID) begin
		insert into PostTopics
			(PostVersionId, TopicId)
		values
			(@PostVersionId, @TopicId)
	end
	else if exists (select null from PostTopics where PostVersionId = @PostVersionId and TopicId = @TopicID and Deleted = 1) begin
		update PostTopics
		set Deleted = 0
		where PostVersionId = @PostVersionId and TopicId = @TopicID
	end
	select @PostNameOut = PostName, @PostDisplayNameOut = PostDisplayName, @TopicNameOut = TopicName, @ConcurencyIdOut = ConcurencyId 
	from PostTopic_View where PostVersionId = @PostVersionId and TopicId = @TopicID

RETURN 0;